﻿# -*- coding: utf-8 -*-
import ToolTranslation
import Transformation
import SceneNode

from History.CmdTranslation import CCmdTranslation
from History.CmdGroupCommand import CCmdGroupCommand
from InputHandler import CInputHandler

class CTranslationTool(CInputHandler):
    def __init__(self):
        """ Default class constructor """
        
        self._handle = 0
        self._startX = 0
        self._startY = 0
        self._startZ = 0
        self._groupCmdRef = None
        
    def Create(self):
        """ Create translation tool """
        
        self._handle = ToolTranslation._New()
        ToolTranslation.Enable(self._handle, False)
        
    def Destroy(self):
        """ Destroy translation tool """
        
        ToolTranslation._Delete(self._handle)
        
    def OnEnter(self, **p):
        """ Enter the translation state """
        
        for ref in SCENE_EDITOR._selectedObjectRefList:
            node = ref()
            if node:
                ToolTranslation.Enable(self._handle, True)
                self.ResetPosition()
                return
                
        ToolTranslation.Enable(self._handle, False)
        SCENE_EDITOR._selectedObjectRefList = []
        
    def ResetPosition(self):
        """ Calculate the center of all selected objects """
        i = 0
        x, y, z = 0, 0, 0
        for ref in SCENE_EDITOR._selectedObjectRefList:
            node = ref()
            if not node:
                continue
                
            tx, ty, tz = Transformation.GetRelativeTranslation(SceneNode._CTransformation(node.GetCoreHandle()))
            x, y, z = x + tx, y + ty, z + tz
            
            i += 1
        
        if i > 0:
            self._startX, self._startY, self._startZ = x / i, y / i, z / i
            ToolTranslation.SetPosition(self._handle, self._startX, self._startY, self._startZ)
            ToolTranslation.Enable(self._handle, True)
        else:
            ToolTranslation.Enable(self._handle, False)
            SCENE_EDITOR._selectedObjectRefList = []
        
    def OnExit(self):
        ToolTranslation.Enable(self._handle, False)
        
    def Update(self):
        ToolTranslation.Update(self._handle)

        try:
            groupCmd = self._groupCmdRef()
        except:
            return
            
        x, y, z = ToolTranslation.GetPosition(self._handle)
        dx, dy, dz = x - self._startX, y - self._startY, z - self._startZ
        for cmd in groupCmd._paramDict['cmdList']:
            cmd.UpdateTranslation(dx, dy, dz)
        
    def Render(self):
        """ Render transform tool """
        
        ToolTranslation.Render(self._handle)
        
    def OnMouseLeftDown(self, e):
        """ Process mouse left down message """
        
        x, y = e.GetPosition()
        ToolTranslation.OnMouseLeftDown(self._handle, x, y)
        
        cmdList = []
        for ref in SCENE_EDITOR._selectedObjectRefList:
            node = ref()
            if not node:
                continue
                
            x, y, z = Transformation.GetRelativeTranslation(SceneNode._CTransformation(node.GetCoreHandle()))
            cmdList.append(CCmdTranslation(nodeRef = ref, newTranslation = (x, y, z)))
            
        cmd = CCmdGroupCommand(cmdList = cmdList)
        HISTORY_MANAGER.RunCommand(cmd)
        
        self._groupCmdRef = HISTORY_MANAGER.GetCurrentCmdRef()
        self._startX, self._startY, self._startZ = ToolTranslation.GetPosition(self._handle)
        
    def OnMouseLeftUp(self, e):
        x, y = e.GetPosition()
        ToolTranslation.OnMouseLeftUp(self._handle, x, y)
        
        self._startX = 0
        self._startY = 0
        self._startZ = 0
        self._groupCmdRef = None        
        
    def OnMouseMotion(self, e):        
        x, y = e.GetPosition()
        ToolTranslation.OnMouseMove(self._handle, x, y)        

__builtins__['TRANSLATION_TOOL'] = CTranslationTool()                   
                            